package com.hn.zhijian.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.zhijian.base.mapper.BaseMapper;
import com.hn.zhijian.base.model.request.PageRequest;
import com.hn.zhijian.base.service.IBaseService;
import com.hn.zhijian.utils.BeanCopier;

import java.util.List;

/**
 * @author carl chen
 * @Classname BaseService
 * @Description 父类接口实现
 * @Date 2019/10/29 18:10
 */
public abstract class BaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService<T> {

    public <T> Page<T> getPaging(PageRequest pageRequest) {

        Page<T> page = new Page<T>(pageRequest.getPage(), pageRequest.getRows());
        // 升序 降序
        page.setAsc(String.valueOf("asc".equalsIgnoreCase(pageRequest.getOrder())));

        return page;
    }

    /**
     * 得到查询器
     * @return
     */
    public QueryWrapper<T> getQueryWrapper(){
        return new QueryWrapper<>();
    }

    public Page convert(Page source, Class destinationClass) {
        List result = BeanCopier.copy(source.getRecords(), destinationClass);
        source.setRecords(result);
        return source;
    }
}
